package com.wlcj.www.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.nutz.ioc.loader.annotation.IocBean;

import com.alibaba.fastjson.JSONObject;
import com.wlcj.util.RedisUtil;
import com.wlcj.www.model.topic.Bbs;
import com.wlcj.www.model.topic.WangQingJu;

@IocBean
public class TopicService {
	public List<WangQingJu> getWqjList(){
		List<WangQingJu> wqjList= new ArrayList<WangQingJu>();
		List<String> list =  RedisUtil.lrange("wqj:api:newtopic", 0, 2);
		for (int i = 0; i < list.size(); i++) {
				WangQingJu wqj = new WangQingJu(); 
			 	JSONObject json = JSONObject.parseObject(list.get(i));
				wqj.setNickname(json.getString("nickname"));
				wqj.setAvatar(json.getString("avatar"));
				wqj.setUrl(json.getString("url"));
				wqj.setContent(json.getString("content"));
				wqj.setReplys(json.getString("replys"));
				wqj.setForwards(json.getString("forwards"));
				wqj.setLastupdate(getString(json.getLong("lastupdate")));
				wqj.setTid(json.getInteger("tid"));
				wqjList.add(i, wqj);
		}
		return wqjList;
	}
	public List<Bbs> getBbsList(){
		List<Bbs> bbsList= new ArrayList<Bbs>();
		List<String> list =  RedisUtil.lrange("bbs:api:newtopic", 0, 6);
		for (int i = 0; i < list.size(); i++) {
				Bbs bbs = new Bbs(); 
				JSONObject json = JSONObject.parseObject(list.get(i));
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				Date date = new Date(json.getLong("dateline")*1000);
				String dd = sdf.format(date).substring(2, sdf.format(date).length()).replaceAll("-", "/");
				bbs.setAuthor(json.getString("author"));
				bbs.setDateline(dd);
				bbs.setTid(json.getInteger("tid"));
				bbs.setTitle(json.getString("title"));
				bbs.setUrl(json.getString("url"));
				bbsList.add(i, bbs);
		}
		return bbsList;
	}
	public String getString(long date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date ddd = new Date(date*1000);
		String dd = sdf.format(ddd);
		String h = dd.substring(5, dd.length()).substring(0, 6).replaceAll("-", "月").trim()+"日";
		String hh = dd.substring(11, 16);
		String result ="";
		if("0".equals(String.valueOf(h.charAt(0)))){
			h = h.substring(1, h.length());
		}
		result = h +" "+hh;
		return result;
	}
}
